// index.js
// 获取应用实例
const app = getApp()
import {
  login,
  home
} from '../../services/index.js'
/**
 * 引入配置
 */
import config from '../../config/index.js'
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    urls : [
      'https://www.runoob.com/try/demo_source/movie.mp4',
      'https://www.runoob.com/try/demo_source/movie.mp4',
      'https://www.runoob.com/try/demo_source/movie.mp4',
      'http://www.jit2008.cn/images/front_end/IEbook002.jpg',
      'https://www.runoob.com/try/demo_source/movie.mp4',
      'https://www.runoob.com/try/demo_source/movie.mp4',
    ],
    videoList:[
    ],
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    this.Get_Video()
  },
  onShow(){

    // this.Get_Big_Picture()
    // this.GetBooks()
  },
  getUserInfo(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  Get_Big_Picture(){
    home.Get_Big_Picture({}).then(res => {
        if(res && res.rows){
            this.setData({
                books:res.rows
            },()=>{
                console.log(this.data.books)
            })
        }
    })
  },
  GetBooks(){
      home.GetBooks({}).then(res => {
          if(res && res.rows){
              this.setData({
                  books:res.rows
              },()=>{
                  console.log(this.data.books)
              })
          }
      })
  },
  Get_Video(){
      home.Get_Video({}).then(res => {
          if(res && res.data){
            for (let v of res.data){
              v.url=config.imageUrl+v.imageUrl
              v.objectFit='contain'
            }
            this.setData({
              videoList:[...res.data,...res.data]
            },()=>{
              console.log(this.data.videoList)
            })
          }
      })
  },
  bindGetUserInfo(){
    console.log('bindGetUserInfo')
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求
          login.OnLogin({code:res.code}).then(res=>{

          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  onPlay(e) {},

  onPause(e) {
    //  console.log('pause', e.detail.activeId)
  },

  onEnded(e) {},

  onError(e) {},

  onWaiting(e) {},

  onTimeUpdate(e) {},

  onProgress(e) {},

  onLoadedMetaData(e) {
    console.log('LoadedMetaData', e)
  },
  gotoList(){
    console.log('gotoList')
    wx.navigateTo({
      url: '/pages/packageA/list/list'
    })
  },
  gotoWeb(){
    console.log('gotoList')
    wx.navigateTo({
      url: '/pages/packageA/web/web'
    })
  },
})
